package cn.fotoit.product.app1.controller;
import cn.fotoit.product.app1.entity.Group;
import cn.fotoit.product.app1.service.GroupService;
import cn.fotoit.product.util.ReturnBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@CrossOrigin
@RequestMapping("group")
public class GroupController {

    @Autowired
    private GroupService groupService;

    @PostMapping("add")
    public ReturnBean add(
            @RequestParam String name,
            @RequestParam String remark) {
        Group group = new Group();
        group.setName(name);
        group.setRemark(remark);
        groupService.add(group);
        return ReturnBean.success("添加成功");
    }

    @PostMapping("updateNotNull")
    public ReturnBean updateNotNull(
            @RequestParam Integer id,
            String name,
            String remark) {
        Group group = new Group();
        group.setId(id);
        group.setName(name);
        group.setRemark(remark);
        groupService.updateNotNull(group);
        return ReturnBean.success("修改成功");
    }

    @PostMapping("deleteById")
    public ReturnBean deleteById(@RequestParam Integer id) {
        groupService.deleteById(id);
        return ReturnBean.success("删除成功");
    }

    @PostMapping("findAll")
    public ReturnBean findAll(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "15") Integer size) {
        return ReturnBean.success("查询成功", groupService.findAll(page, size));
    }

    @PostMapping("findById")
    public ReturnBean findById(
            @RequestParam Integer id) {
        return ReturnBean.success("查询成功", groupService.findById(id));
    }


}
